package com.crane.oss.web.controller.sys;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crane.common.core.dto.MultiResponse;
import com.crane.common.core.dto.Response;
import com.crane.common.core.exception.BizException;
import com.crane.oss.sys.bean.request.dict.AddDictReq;
import com.crane.oss.sys.bean.request.dict.UpdateDictReq;
import com.crane.oss.sys.bean.request.dictdetail.AddDictDetailReq;
import com.crane.oss.sys.bean.request.dictdetail.UpdateDictDetailReq;
import com.crane.oss.sys.bean.request.dict.QueryDictDetailReq;
import com.crane.oss.sys.bean.request.dict.QueryDictReq;
import com.crane.oss.sys.bean.resp.DictDetailResp;
import com.crane.oss.sys.bean.resp.DictListResp;
import com.crane.oss.sys.service.DictDetailService;
import com.crane.oss.sys.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author bealon
 * @email ccsu123456@qq.com
 * @date 2019年01月08日 19时38分36秒
 */

@Api(value = "DictController", tags = {"系统管理-字典管理"})
@RestController
@RequestMapping("/sys/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @Autowired
    DictDetailService dictDetailService;

    @ApiOperation(value = "获取字典组列表信息", notes = "获取列表信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryDictReq")
    @RequiresPermissions("sys:dict:list")
    @PostMapping("/list")
    public MultiResponse<DictListResp> list(@RequestBody QueryDictReq request) throws BizException {
        IPage<DictListResp> page = dictService.queryList(request);
        return MultiResponse.of(page.getRecords(), (int) page.getTotal(), request);
    }

    @ApiOperation(value = "获取字典明细列表信息", notes = "获取列表信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryDictDetailReq")
    @RequiresPermissions("sys:dict:list")
    @PostMapping("/listDetail")
    public MultiResponse<DictDetailResp> listDetail(@RequestBody QueryDictDetailReq request) throws BizException {
        IPage<DictDetailResp> page = dictDetailService.queryList(request);
        return MultiResponse.of(page.getRecords(), (int) page.getTotal(), request);
    }


    @ApiOperation(value = "新增字典组", notes = "获取新增信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "AddDictReq")
    @RequiresPermissions("sys:dict:save")
    @PostMapping("/saveDict")
    public Response saveDict(@RequestBody @Valid AddDictReq request) {
        int count = dictService.save(request);
        return Response.buildSuccess();
    }

    @ApiOperation(value = "新增明细", notes = "获取新增信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "AddDictDetailReq")
    @RequiresPermissions("sys:dict:save")
    @PostMapping("/saveDictDetail")
    public Response saveDictDetail(@RequestBody @Valid AddDictDetailReq request) {
        int count = dictDetailService.save(request);
        return Response.buildSuccess();
    }

    @ApiOperation(value = "修改字典组", notes = "修改")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "UpdateDictReq")
    @RequiresPermissions("sys:dict:update")
    @PostMapping("/updateDict")
    public Response updateDict(@RequestBody @Valid UpdateDictReq request) {
        int count = dictService.update(request);
        return Response.buildSuccess();
    }


    @ApiOperation(value = "修改明细", notes = "修改")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "UpdateDictDetailReq")
    @RequiresPermissions("sys:dict:update")
    @PostMapping("/updateDictDetail")
    public Response updateDictDetail(@RequestBody @Valid UpdateDictDetailReq request) {
        int count = dictDetailService.update(request);
        return Response.buildSuccess();
    }


    @ApiOperation(value = "删除字典组", notes = "删除字典组")
    @ApiImplicitParam(name = "id", value = "请求对象", paramType = "path", dataType = "Long")
    @RequiresPermissions("sys:dict:delete")
    @GetMapping("/deleteDict/{id}")
    public Response deleteDict(@PathVariable("id") Long id) {
        int count = dictService.delete(id);
        return Response.buildSuccess();
    }

    @ApiOperation(value = "删除明细", notes = "删除明细")
    @ApiImplicitParam(name = "id", value = "请求对象", paramType = "path", dataType = "Long")
    @RequiresPermissions("sys:dict:delete")
    @GetMapping("/deleteDictDetail/{id}")
    public Response deleteDictDetail(@PathVariable("id") Long id) {
        int count = dictDetailService.delete(id);
        return Response.buildSuccess();
    }

}
